package com.yanqu.road.server.logic.activity.condition.rank;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserHistoryAttribute;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.EarnSpeedModule;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseRankActivityCondition;
import com.yanqu.road.server.manger.cross3.ChannelRankActivityMgr;

import java.math.BigInteger;

/**
 * 赚速提升冲榜 冲榜活动
 */
public class EarnSpeedRiseRankCondition extends BaseRankActivityCondition {

	private boolean isFirstChange = false;

	public EarnSpeedRiseRankCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
		super(activityInfo, activityConditionInfo, baseActivityData);
	}

	@Override
	public void addTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.EarnSpeedRiseRank.getValue(), handle);
	}

	@Override
	public void removeTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.EarnSpeedRiseRank.getValue(), handle);
	}

	@Override
	public void initSetPreValue() {
		UserHistoryAttribute userHistoryAttribute = player.getModule(PlayerModule.class).getUserHistoryAttribute();
		if(BigInteger.ZERO.compareTo(userHistoryAttribute.getEarnSpeed()) == 0){//修数据后，需要重新初始化赚速
			player.getModule(EarnSpeedModule.class).scheduleAddSilver();
		}
		BigInteger topEarnSpeed = userHistoryAttribute.getEarnSpeed();
		userHistoryAttribute.setActivityStartEarnSpeed(topEarnSpeed);
		setPreValue(topEarnSpeed);
		player.getModule(PlayerModule.class).syncUserHistoryAttribute();
	}

	@Override
	protected void player_BoardLevelHandle(Object object) {
		BigInteger args = (BigInteger) object;
		BigInteger oldValue = getValue();
		setValue(args.subtract(getPreValue()));
		if(!isFirstChange || getValue().compareTo(oldValue) != 0) {

			if (activityInfo.getType() == eActivityType.ChannelRankActivity.getValue()) {
				ChannelRankActivityMgr.userRankChange(activityInfo.getActivityId(), activityConditionInfo.getType(), getValue().longValue(), player.getUserId(),"");
				ChannelRankActivityMgr.channelRankPartChange(activityInfo.getActivityId(), eGamePlayerEventType.ServerEarnSpeedRiseRank.getValue(), getValue().longValue(), player.getUserId());
			} else {
				rankChange();
			}

			isFirstChange = true;
		}
	}
}
